home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / Dave Mark / Mac C Primer 1 (SC++7) / 7.06 - PrintPICT / PrintPICT.c next >
Encoding:
C/C++ Source or Header  |  1994-05-13  |  3.5 KB  |  172 lines  |  [TEXT/KAHL]

  1. /********************************************************/
  2. /*                                                        */
  3. /*  PrintPICT Code from Chapter Seven of                */
  4. /*                                                        */
  5. /*    *** The Macintosh Programming Primer, 2nd Ed. ***    */
  6. /*                                                      */
  7. /*    Copyright 1992, Dave Mark and Cartwright Reed       */
  8. /*                                                        */
  9. /********************************************************/
  10.  
  11. #include <Printing.h>
  12.  
  13. #define kBaseResID            128
  14.  
  15. #define kDontScaleOutput    nil
  16.  
  17. #define    kEmptyString        "\p"
  18. #define kNilFilterProc        nil
  19.  
  20. #define    kErrorAlertID        kBaseResID
  21.  
  22.  
  23. /***************/
  24. /*  Functions  */
  25. /***************/
  26.  
  27. void        ToolBoxInit( void );
  28. PicHandle    LoadPICT( void );
  29. THPrint        PrintInit( void );
  30. Boolean        DoDialogs( THPrint printRecordH );
  31. void        PrintPicture( PicHandle picture, THPrint printRecordH );
  32. void        CenterPict( PicHandle picture, Rect *destRectPtr );
  33. void        DoError( Str255 errorString, Boolean fatal );
  34.  
  35.  
  36. /**************************** main **********************/
  37.  
  38. void    main( void )
  39. {
  40.     PicHandle    picture;
  41.     THPrint        printRecordH;
  42.     
  43.     ToolBoxInit();
  44.     
  45.     picture = LoadPICT();
  46.     printRecordH = PrintInit();
  47.     
  48.     if ( DoDialogs( printRecordH ) )
  49.         PrintPicture( picture, printRecordH );
  50. }
  51.  
  52.  
  53. /****************** ToolBoxInit *********************/
  54.  
  55. void    ToolBoxInit( void )
  56. {
  57.     InitGraf( &qd.thePort );
  58.     InitFonts();
  59.     InitWindows();
  60.     InitMenus();
  61.     TEInit();
  62.     InitDialogs( 0L );
  63.     InitCursor();
  64. }
  65.  
  66.  
  67. /******************************** LoadPICT *********/
  68.  
  69. PicHandle    LoadPICT( void )
  70. {
  71.     PicHandle    picture;
  72.     
  73.     picture = GetPicture( kBaseResID );
  74.     
  75.     if ( picture == nil )
  76.         DoError( "\pCan't load PICT resource...", true );
  77.  
  78.     return( picture );
  79. }
  80.  
  81.  
  82. /******************************** PrintInit *********/
  83.  
  84. THPrint    PrintInit( void )
  85. {
  86.     THPrint    printRecordH;
  87.     
  88.     printRecordH = (THPrint)NewHandle( sizeof( TPrint ) );
  89.     
  90.     if ( printRecordH == nil )
  91.         DoError( "\pNot enough memory to allocate print record", true );
  92.  
  93.     PrOpen();
  94.     PrintDefault( printRecordH );
  95.     
  96.     return( printRecordH );
  97. }
  98.  
  99.  
  100. /********************************    DoDialogs    *******/
  101.  
  102. Boolean    DoDialogs( THPrint    printRecordH )
  103. {
  104.     Boolean    confirmed = true;
  105.     
  106.     confirmed = PrStlDialog( printRecordH );
  107.     
  108.     if ( confirmed )
  109.         confirmed = PrJobDialog( printRecordH );
  110.     
  111.     return( confirmed );
  112. }
  113.  
  114.  
  115. /********************************    PrintPicture    *******/
  116.  
  117. void    PrintPicture( PicHandle picture, THPrint printRecordH )
  118. {
  119.     TPPrPort    printPort;
  120.     Rect        pictureRect;
  121.     TPrStatus    printStatus;
  122.     
  123.     printPort = PrOpenDoc( printRecordH, nil, nil );
  124.     
  125.     PrOpenPage( printPort, kDontScaleOutput );
  126.     
  127.     if ( PrError() != noErr )
  128.         DoError( "\pError returned by PrOpenPage()...", true );
  129.     
  130.     pictureRect = (**printRecordH).prInfo.rPage;
  131.     
  132.     CenterPict( picture, &pictureRect );
  133.     DrawPicture( picture, &pictureRect );
  134.     
  135.     PrClosePage( printPort );
  136.     PrCloseDoc( printPort );
  137.     
  138.     if ( (**printRecordH).prJob.bJDocLoop == bSpoolLoop )
  139.         PrPicFile( printRecordH, nil, nil, nil, &printStatus );
  140.     
  141.     PrClose();
  142.     DisposHandle( (Handle)printRecordH );
  143. }
  144.  
  145.  
  146. /****************** CenterPict ********************/
  147.  
  148. void    CenterPict( PicHandle picture, Rect *destRectPtr )
  149. {
  150.     Rect    windRect, pictRect;
  151.     
  152.     windRect = *destRectPtr;
  153.     pictRect = (**( picture )).picFrame;
  154.     OffsetRect( &pictRect, windRect.left - pictRect.left,
  155.                            windRect.top     - pictRect.top);
  156.     OffsetRect( &pictRect,(windRect.right - pictRect.right)/2,
  157.                           (windRect.bottom - pictRect.bottom)/2);
  158.     *destRectPtr = pictRect;
  159. }
  160.  
  161.  
  162. /***************** DoError ********************/
  163.  
  164. void    DoError( Str255 errorString, Boolean fatal )
  165. {
  166.     ParamText( errorString, kEmptyString, kEmptyString, kEmptyString );
  167.     
  168.     StopAlert( kErrorAlertID, kNilFilterProc );
  169.     
  170.     if ( fatal )
  171.         ExitToShell();
  172. }